//package com.org.config.interceptor;
//
//import com.google.gson.JsonObject;
//import com.org.annotation.ValidateToken;
//import com.org.config.GuavaCacheConfig;
//import com.org.constant.Params;
//import com.org.constant.ResponseEnum;
//import com.org.exception.RespException;
//import com.org.util.JsonUtils;
//import com.org.util.JwtUtils;
//import com.org.util.Utils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.method.HandlerMethod;
//import org.springframework.web.servlet.HandlerInterceptor;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.lang.reflect.Method;
//
///**
// * @author: vane.Q
// * @date: 2022/12/4 10:32
// */
//public class TokenInterceptor implements HandlerInterceptor {
//
//
//    @Autowired
//    private GuavaCacheConfig guavaCacheConfig;
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
//                             Object handler) throws Exception {
//        // 从 http 请求头中取出 token
//        String token = request.getHeader("token");
//        // 如果不是映射到方法直接通过
//        if (!(handler instanceof HandlerMethod)) {
//            return true;
//        }
//        HandlerMethod handlerMethod = (HandlerMethod) handler;
//        Method method = handlerMethod.getMethod();
//        //检查是否有 validateToken 注释，有则跳过认证
//        if (method.isAnnotationPresent(ValidateToken.class)) {
//            ValidateToken passToken = method.getAnnotation(ValidateToken.class);
//            if (passToken.required()) {
//                if (token == null){
//                    throw new RespException(ResponseEnum.ERR_HANDLER_TOKEN_EXPAIRE);
//                }
//                JsonObject tokenJson = JsonUtils.stringToJsonObject(token);
//                String userId = JsonUtils.getAsString(tokenJson, Params.USER_ID);
//                this.validateUserInfo(userId);
//                String userName = JsonUtils.getAsString(tokenJson, Params.USER_NAME);
//                String jwt = JsonUtils.getAsString(tokenJson, Params.JWT);
//                String decode = JwtUtils.decode(jwt);
//                JsonObject decodeJson = JsonUtils.stringToJsonObject(decode);
//                String decodeUserId =  JsonUtils.getAsString(decodeJson, Params.USER_ID);
//                String decodeUserName = JsonUtils.getAsString(tokenJson, Params.USER_NAME);
//                if (!userId.equals(decodeUserId) && !userName.equals(decodeUserName)) {
//                    throw new RespException(ResponseEnum.ERR_HANDLER_TOKEN_FAKE);
//                }
//                return true;
//            }
//        }
//        return true;
//    }
//
//    private void validateUserInfo(String userId) {
//        String token = (String) guavaCacheConfig.get(Utils.getTokenKey(userId));
//        if (token == null) {
//            throw new RespException(ResponseEnum.ERR_HANDLER_TOKEN_ERROR);
//        }
//    }
//}
